package com.zut.controller;

import com.github.pagehelper.PageInfo;
import com.zut.domain.Coach;
import com.zut.domain.OrderPractise;
import com.zut.service.OrderPractiseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@CrossOrigin
@RestController
@RequestMapping("/order")
public class OrderPractiseController {

    @Autowired
    private OrderPractiseService orderPractiseService;

    /*得到所有预约练车信息*/
    @GetMapping("/{pageNum}/{pageSize}")
    public Result getAll(@PathVariable Integer pageNum, @PathVariable Integer pageSize) {
        PageInfo<OrderPractise> orderPractisePageInfo = orderPractiseService.getAll(pageNum, pageSize);
        Integer code = orderPractisePageInfo.getSize() != 0 ? Code.GET_OK : Code.GET_ERR;
        String msg = orderPractisePageInfo.getSize() != 0 ? "" : "数据查询失败，请重试！";
        return new Result(code, orderPractisePageInfo, msg);
    }

    /*预约练车*/
    @PostMapping
    public Result insert(@RequestBody OrderPractise orderPractise) {
        boolean flag = orderPractiseService.insert(orderPractise);
        return new Result(flag ? Code.SAVE_OK : Code.SAVE_ERR, flag);
    }

    /*根据id删除预约练车信息*/
    @DeleteMapping("/{id}")
    public Result deleteById(@PathVariable String id) {
        boolean flag = orderPractiseService.delete(id);
        return new Result(flag ? Code.DELETE_OK : Code.DELETE_ERR, flag);
    }

    /*修改预约练车信息*/
    @PutMapping
    public Result Update(@RequestBody OrderPractise orderPractise) {
        boolean flag = orderPractiseService.update(orderPractise);
        return new Result(flag ? Code.UPDATE_OK : Code.UPDATE_ERR, flag);
    }

    /*得到所有已预约练车信息*/
    @GetMapping("/reserved/{pageNum}/{pageSize}")
    public Result reserved(@PathVariable Integer pageNum, @PathVariable Integer pageSize) {
        PageInfo<OrderPractise> orderPractisePageInfo = orderPractiseService.reserved(pageNum, pageSize);
        Integer code = orderPractisePageInfo.getSize() != 0 ? Code.GET_OK : Code.GET_ERR;
        String msg = orderPractisePageInfo.getSize() != 0 ? "" : "数据查询失败，请重试！";
        return new Result(code, orderPractisePageInfo, msg);
    }

    /*根据id查询所有预约*/
    @GetMapping("/order/{pageNum}/{pageSize}/{id}")
    public Result getById(@PathVariable Integer pageNum, @PathVariable Integer pageSize, @PathVariable String id) {
        PageInfo<Map<String, String>> orderPractisePageInfo = orderPractiseService.getById(pageNum, pageSize, id);
        Integer code = orderPractisePageInfo.getSize() != 0 ? Code.GET_OK : Code.GET_ERR;
        String msg = orderPractisePageInfo.getSize() != 0 ? "" : "数据查询失败，请重试！";
        return new Result(code, orderPractisePageInfo, msg);
    }

//    /*根据用户id获得已预约练车场地的id*/
//    @GetMapping("/reserved/{id}")
//    public Result reservedId(@PathVariable String id) {
//        String s = orderPractiseService.reservedId(id);
//        Integer code = s != null ? Code.GET_OK : Code.GET_ERR;
//        String msg = s != null ? "查询成功" : "无预约数据";
//        return new Result(code,s,msg);
//
//    }
}
